home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / byobu / battery < prev    next >
Text File  |  2009-10-11  |  2KB  |  78 lines

  1. #!/bin/sh -e
  2. #
  3. #  battery: print the state of the battery
  4. #  Copyright (C) 2009 Rapha├½l Pinson.
  5. #
  6. #  Authors: Rapha├½l Pinson <raphink@ubuntu.com>
  7. #           Dustin Kirkland <kirkland@canonical.com>
  8. #
  9. #  This program is free software: you can redistribute it and/or modify
  10. #  it under the terms of the GNU General Public License as published by
  11. #  the Free Software Foundation, version 3 of the License.
  12. #
  13. #  This program is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #  GNU General Public License for more details.
  17. #
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  
  21.  
  22. search () {
  23.     local str expr
  24.     str="$1"
  25.     expr="$2"
  26.     echo "$str" | sed -n "s/${expr}/\1/p"
  27. }
  28.  
  29.  
  30. if [ "$1" = "--detail" ]; then
  31.     for bat in $(ls /proc/acpi/battery); do
  32.         cat "/proc/acpi/battery/$bat/info"
  33.         cat "/proc/acpi/battery/$bat/state"
  34.     done
  35.     exit 0
  36. fi
  37.  
  38. for bat in $(ls /proc/acpi/battery); do
  39.     # make sure that this battery is present
  40.     infofile=$(cat "/proc/acpi/battery/$bat/info")
  41.     present=$(search "$infofile" "present: *\(.*\)")
  42.     [ "${present}" = "no" ] && continue
  43.  
  44.     # obtain full and remaining battery values
  45.     statefile=$(cat "/proc/acpi/battery/$bat/state")
  46.     full=$(search "$infofile" "last full capacity: *\(.*\) m[AW]h")
  47.     rem=$(search "$statefile" "remaining capacity: *\(.*\) m[AW]h")
  48.  
  49.     percent=$( echo "$rem" "$full" | awk '{printf "%.0f",  100*$1/$2}')
  50.     if [ "$percent" -lt 33 ]; then
  51.         per_color="Rk"
  52.     elif [ "$percent" -lt 67 ]; then
  53.         per_color="Yk"
  54.     else
  55.         per_color="Gk"
  56.     fi
  57.     percent="$percent%"
  58.  
  59.     state=$(search "$statefile" "charging state: *\(.*\)")
  60.     case $state in
  61.         charging)
  62.             sign="+"
  63.         ;;
  64.         discharging)
  65.             sign="-"
  66.         ;;
  67.         charged)
  68.             sign="="
  69.             percent=
  70.         ;;
  71.         *)
  72.             sign="$state"
  73.         ;;
  74.     esac
  75.     printf "\005{=b %s}%s\005{-}\005{= %s}|%s|\005{-} " "$per_color" "$percent" "$per_color" "$sign"
  76.     break
  77. done
  78.